Aprendiendo a graficar mediante casos de la vida real.

En este espacio vamos a hacer mapas con R.

1. Activar las librerias

library(ggplot2) #Esta librería nos permitira hacer graficas bonitas 
library(ggmap) #Esta librería nos permitirá hacer mapas 
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(plotly) #esta grafica nos permitira hacer graficas interactivas 
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggmap':
## 
##     wind
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Caso 100%real no feik

2. Caso de infidelidad. Supongamos que necesitas saber los lugares donde estuvo tu pareja en los “n” días, pero lo unicos datos que pudiste obtener son los de su gps. Entonces con su longitud y latitud se podrá cononcer los lugares exactos donde estuvo.

Día<-1:7
LONGITUD<-sample(seq(-156.7,-156.6,0.01), 7,T)
LATITUD<-sample(seq(20.8,21.9,0.01), 7, T)
gps<-data.frame(Día,LONGITUD,LATITUD)
gps
##   Día LONGITUD LATITUD
## 1   1  -156.67   21.67
## 2   2  -156.60   21.89
## 3   3  -156.69   21.86
## 4   4  -156.70   20.91
## 5   5  -156.60   20.85
## 6   6  -156.67   21.19
## 7   7  -156.62   21.15

3. Con esto hemos obtenido los puntos en lo que tu pareja estuvo, por lo que ahora pasaremos a graficarlos con ‘ggplot’ y ‘geom_point’

ruta_del_infiel<-ggplot(gps)+geom_point(aes(x=LONGITUD,y=LATITUD),color=Día)
ruta_del_infiel

4. Ahora vamos a identificar los lugares con los numeros de los días en lo que estuvo en Hawaii

ruta_del_infiel_con_días<-ggplot(gps)+geom_text(aes(LONGITUD,LATITUD,label = Día))
ruta_del_infiel_con_días

5. El siguiente paso será hacer una grafica interactiva con ‘ggploty’

ggplotly(ruta_del_infiel_con_días)

6. Graficando los datos de la ruta del infiel en un mapa con la función ‘qmplot’

La funcion ‘qmplot’ nos permite hacer mas de forma rapida y sencilla conociendo la longitud y la latitud.

qmplot(LONGITUD,LATITUD, data = gps)
## Using zoom = 13...
## 84 tiles needed, this may take a while (try a smaller zoom).
## Source : http://tile.stamen.com/terrain/13/530/3584.png
## Source : http://tile.stamen.com/terrain/13/531/3584.png
## Source : http://tile.stamen.com/terrain/13/532/3584.png
## Source : http://tile.stamen.com/terrain/13/530/3585.png
## Source : http://tile.stamen.com/terrain/13/531/3585.png
## Source : http://tile.stamen.com/terrain/13/532/3585.png
## Source : http://tile.stamen.com/terrain/13/530/3586.png
## Source : http://tile.stamen.com/terrain/13/531/3586.png
## Source : http://tile.stamen.com/terrain/13/532/3586.png
## Source : http://tile.stamen.com/terrain/13/530/3587.png
## Source : http://tile.stamen.com/terrain/13/531/3587.png
## Source : http://tile.stamen.com/terrain/13/532/3587.png
## Source : http://tile.stamen.com/terrain/13/530/3588.png
## Source : http://tile.stamen.com/terrain/13/531/3588.png
## Source : http://tile.stamen.com/terrain/13/532/3588.png
## Source : http://tile.stamen.com/terrain/13/530/3589.png
## Source : http://tile.stamen.com/terrain/13/531/3589.png
## Source : http://tile.stamen.com/terrain/13/532/3589.png
## Source : http://tile.stamen.com/terrain/13/530/3590.png
## Source : http://tile.stamen.com/terrain/13/531/3590.png
## Source : http://tile.stamen.com/terrain/13/532/3590.png
## Source : http://tile.stamen.com/terrain/13/530/3591.png
## Source : http://tile.stamen.com/terrain/13/531/3591.png
## Source : http://tile.stamen.com/terrain/13/532/3591.png
## Source : http://tile.stamen.com/terrain/13/530/3592.png
## Source : http://tile.stamen.com/terrain/13/531/3592.png
## Source : http://tile.stamen.com/terrain/13/532/3592.png
## Source : http://tile.stamen.com/terrain/13/530/3593.png
## Source : http://tile.stamen.com/terrain/13/531/3593.png
## Source : http://tile.stamen.com/terrain/13/532/3593.png
## Source : http://tile.stamen.com/terrain/13/530/3594.png
## Source : http://tile.stamen.com/terrain/13/531/3594.png
## Source : http://tile.stamen.com/terrain/13/532/3594.png
## Source : http://tile.stamen.com/terrain/13/530/3595.png
## Source : http://tile.stamen.com/terrain/13/531/3595.png
## Source : http://tile.stamen.com/terrain/13/532/3595.png
## Source : http://tile.stamen.com/terrain/13/530/3596.png
## Source : http://tile.stamen.com/terrain/13/531/3596.png
## Source : http://tile.stamen.com/terrain/13/532/3596.png
## Source : http://tile.stamen.com/terrain/13/530/3597.png
## Source : http://tile.stamen.com/terrain/13/531/3597.png
## Source : http://tile.stamen.com/terrain/13/532/3597.png
## Source : http://tile.stamen.com/terrain/13/530/3598.png
## Source : http://tile.stamen.com/terrain/13/531/3598.png
## Source : http://tile.stamen.com/terrain/13/532/3598.png
## Source : http://tile.stamen.com/terrain/13/530/3599.png
## Source : http://tile.stamen.com/terrain/13/531/3599.png
## Source : http://tile.stamen.com/terrain/13/532/3599.png
## Source : http://tile.stamen.com/terrain/13/530/3600.png
## Source : http://tile.stamen.com/terrain/13/531/3600.png
## Source : http://tile.stamen.com/terrain/13/532/3600.png
## Source : http://tile.stamen.com/terrain/13/530/3601.png
## Source : http://tile.stamen.com/terrain/13/531/3601.png
## Source : http://tile.stamen.com/terrain/13/532/3601.png
## Source : http://tile.stamen.com/terrain/13/530/3602.png
## Source : http://tile.stamen.com/terrain/13/531/3602.png
## Source : http://tile.stamen.com/terrain/13/532/3602.png
## Source : http://tile.stamen.com/terrain/13/530/3603.png
## Source : http://tile.stamen.com/terrain/13/531/3603.png
## Source : http://tile.stamen.com/terrain/13/532/3603.png
## Source : http://tile.stamen.com/terrain/13/530/3604.png
## Source : http://tile.stamen.com/terrain/13/531/3604.png
## Source : http://tile.stamen.com/terrain/13/532/3604.png
## Source : http://tile.stamen.com/terrain/13/530/3605.png
## Source : http://tile.stamen.com/terrain/13/531/3605.png
## Source : http://tile.stamen.com/terrain/13/532/3605.png
## Source : http://tile.stamen.com/terrain/13/530/3606.png
## Source : http://tile.stamen.com/terrain/13/531/3606.png
## Source : http://tile.stamen.com/terrain/13/532/3606.png
## Source : http://tile.stamen.com/terrain/13/530/3607.png
## Source : http://tile.stamen.com/terrain/13/531/3607.png
## Source : http://tile.stamen.com/terrain/13/532/3607.png
## Source : http://tile.stamen.com/terrain/13/530/3608.png
## Source : http://tile.stamen.com/terrain/13/531/3608.png
## Source : http://tile.stamen.com/terrain/13/532/3608.png
## Source : http://tile.stamen.com/terrain/13/530/3609.png
## Source : http://tile.stamen.com/terrain/13/531/3609.png
## Source : http://tile.stamen.com/terrain/13/532/3609.png
## Source : http://tile.stamen.com/terrain/13/530/3610.png
## Source : http://tile.stamen.com/terrain/13/531/3610.png
## Source : http://tile.stamen.com/terrain/13/532/3610.png
## Source : http://tile.stamen.com/terrain/13/530/3611.png
## Source : http://tile.stamen.com/terrain/13/531/3611.png
## Source : http://tile.stamen.com/terrain/13/532/3611.png

### Podemos cambiar el color y tamaño de los puntos de nuestro mapa agregando los parametros ‘color’ y’size’ : # 7. Tuneando el mapa.

qmplot(LONGITUD,LATITUD,data = gps,color = Día, size = I(2))
## Using zoom = 13...
## 84 tiles needed, this may take a while (try a smaller zoom).

8. Mapa de densidad

Podemos agregar el parametro ‘geo=c(“point”, “density2d”)’ para hacer un mapa de puntos y densidad.:

qmplot(LONGITUD,LATITUD,data = gps,geom = c("point","density2d"))
## Using zoom = 13...
## 84 tiles needed, this may take a while (try a smaller zoom).

# Tutorial